package com.sino.second.business.service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sino.bean.Basicinfo;
import com.sino.bean.TblZhuanjiarukuxinxibiao;

import com.sino.model.BasicInfoModel;
import com.sino.model.ExpertModel;
import com.sino.second.bean.SysXmpszb;
import com.sino.second.bean.TblSystemparam;
import com.sino.second.bean.TblXmfs1;
import com.sino.second.bean.TblXmjg1;
import com.sino.second.bean.TblXmpfbz;
import com.sino.second.business.IReviewService;
import com.sino.second.dao.IExpertsReviewDAO;
import com.sino.second.model.ExpertsReviewModel;
import com.sino.second.model.ExpertsReviewModel1;
import com.sino.second.model.ExpertsReviewModel2;
import com.sino.second.model.XmReviewResults;
import com.sino.second.model.XmReviewResults2;

import com.sino.second.util.BeanAndModelConverter;
import com.sino.util.DateUtils;
import com.sino.util.page.PagerModel;

public class Review1ServiceImpl implements IReviewService {

	public int findCountReviewXmFsList(String hqlWhere) {
		return this.review1DAO.findReviewCount(hqlWhere);
	}

	public List<ExpertsReviewModel2> findReviewXmFsList(String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	private IExpertsReviewDAO review1DAO;

	public ExpertsReviewModel poToVo(Object oepm) {
		TblXmfs1 tx1 = (TblXmfs1) oepm;
		return BeanAndModelConverter.getExpertsReviewModel1(tx1);

	}

	/**
	 * 初审专家评分VO转PO
	 */
	public Object voToPo(ExpertsReviewModel erm) {
		TblXmfs1 tx1 = new TblXmfs1();
		if (erm.getId() == null || erm.equals("")) {

		} else {
			tx1.setId(erm.getId());
		}
		Basicinfo bi = new Basicinfo();
		bi.setZlid(erm.getBasicInfoModel().getZlid());
		tx1.setBasicinfo(bi);
		TblZhuanjiarukuxinxibiao tzjrk = new TblZhuanjiarukuxinxibiao();
		tzjrk.setFBh(erm.getExpertModel().getFBh());
		tx1.setTblZhuanjiarukuxinxibiao(tzjrk);
		tx1.setFzqz(erm.getFzqz());
		tx1.setState(erm.getState());
		SysXmpszb sx = new SysXmpszb();
		sx.setId(erm.getXmpszbModel().getId());
		tx1.setSysXmpszb(sx);
		TblXmpfbz txf = new TblXmpfbz();
		txf.setId(erm.getXmpsbzModel().getId());
		tx1.setTblXmpfbz(txf);
		tx1.setXmfs(erm.getXmfs());
		tx1.setYear(new Integer(Calendar.getInstance().get(Calendar.YEAR))
				.toString());
		tx1.setPsrq(DateUtils.getSystemDate());
		return tx1;
	}

	public List<ExpertsReviewModel> findReviewsList(String zlid, Integer zjid) {
		List<TblXmfs1> tx1List = this.getReview1DAO().findReviewsList(zlid,
				zjid);
		List<ExpertsReviewModel> ermList = new ArrayList<ExpertsReviewModel>();
		for (TblXmfs1 txf1 : tx1List) {
			ermList.add(BeanAndModelConverter.getExpertsReviewModel1(txf1));
		}
		return ermList;
	}

	/**
	 * 保存初审项目评分信息tx1List
	 * 
	 * @param o
	 *            ermList信息
	 */
	public void saveOrUpdateReviews(Object o) {
		List<ExpertsReviewModel> ermList = (List<ExpertsReviewModel>) o;
		List<TblXmfs1> tx1List = new ArrayList<TblXmfs1>();
		for (ExpertsReviewModel erm : ermList) {
			tx1List.add((TblXmfs1) this.voToPo(erm));
		}
		this.getReview1DAO().saveOrUpdateReviews(tx1List);

	}
	/**
	 * xyw 设置申报时间
	 */
	public void saveOrUpdateTime(TblSystemparam t,TblSystemparam t2) {
		this.review1DAO.saveOrUpdateTime(t,t2);
	}
	
	
	

	/**
	 * 专家列表记录数
	 * 
	 * @param xk
	 *            学科
	 * @param xkId
	 *            专利ID
	 * @return
	 */
	public int findReviewXmCount(String xkId, String hqlWhere) {
		return this.getReview1DAO().findReviewXmCount(xkId, hqlWhere);
	}

	public IExpertsReviewDAO getReview1DAO() {
		return review1DAO;
	}

	public void setReview1DAO(IExpertsReviewDAO review1DAO) {
		this.review1DAO = review1DAO;
	}

	public int findReviewCount(String hqlWhere) {
		return this.getReview1DAO().findReviewCount(hqlWhere);

	}

	public Object findReviewResultsByZlid(String zjid) {

		return null;
	}

	public List<ExpertsReviewModel> findReviewXmListByZlId(String zlid,
			String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	public List findReviewXmListByZjId(Integer zjid, String hqlWhere) {
		List<Object[]> zjoList = this.getReview1DAO().findReviewXmListByZjId(
				zjid, hqlWhere);
		List<ExpertsReviewModel> ermList = new ArrayList<ExpertsReviewModel>();
		for (Object[] o : zjoList) {
			ExpertsReviewModel1 erm = new ExpertsReviewModel1();
			erm.setBasicInfoModel(BeanAndModelConverter
					.getBasicInfoModel((Basicinfo) o[0]));
			erm.setXmzf((Double) o[1]);
			erm.setFzqzzf((Double) o[2]);
			ermList.add(erm);

		}
		return ermList;
	}

	/**
	 * xyw 看是否被复审 如果没有进行初审
	 * 
	 * @param hqlWhere
	 * @return
	 */

	public int findReviewXkId(String hqlWhere) {
		int c = this.review1DAO.findCountReviewXmFsList(hqlWhere);
		if (c > 0) {
			return c;
		} else {
			return 0;
		}
	}

	/**
	 * xyw 初审专业信息列表
	 */
	public List findReviewXmList(int firstRow, int maxRow, String xkId,
			String hqlWhere) {
		// TODO Auto-generated method stub
		Double orderByFenshu = 0.0;
		Double sumOrderScore = 0.0;
		Double sumFenshu = 0.0;
		String ispass = null;
		List<BasicInfoModel> ermList10 = new ArrayList<BasicInfoModel>();
		BasicInfoModel basicModel;
		// 根据学科查找此学科里所有的项目
		List<Basicinfo> tx1list2 = this.getReview1DAO().findReviewXmList(
				firstRow, maxRow, xkId, hqlWhere);
		// 循环项目集合
		for (int i = 0; i < tx1list2.size(); i++) {
			basicModel = new BasicInfoModel();
			Basicinfo info = tx1list2.get(i);
			// 单个项目的zlid
			String zlid = tx1list2.get(i).getZlid();
			// 根据zlid查出该项目被所有专家打的总分（一）
			if (null != this.getReview1DAO().findSumFenshuByZlid(zlid)) {
				sumFenshu = (Double) this.getReview1DAO().findSumFenshuByZlid(
						zlid);
			} else {
				sumFenshu = 0.0;
			}
			// 根据此zlid查出有为此项目打分的专家个数count（二）
			int count = this.getReview1DAO().findCountZhuanjiaByZlid(zlid);
			// 求出平均分（三）
			if (sumFenshu == 0.0 || count == 0) {
				basicModel.setAvgFenshu(0.0);
			} else {
				// 求平均分留2位小数点
				double avg = sumFenshu / (count + 0.0);
				BigDecimal b = new BigDecimal(avg);
				double avg1 = b.setScale(2, BigDecimal.ROUND_HALF_UP)
						.doubleValue();
				basicModel.setAvgFenshu(avg1);
			}
			// 根据zlid查排序分（四）
			Map map = this.YY(zlid, hqlWhere);
			if (count == 0) {
				orderByFenshu = 0.0;
			} else {
				// 根据zlid查找总排序分
				sumOrderScore = (Integer) map.get(zlid) + 0.0;
				// 平均排序分

				double avg2 = sumOrderScore / (count + 0.0);
				BigDecimal b = new BigDecimal(avg2);
				double avg3 = b.setScale(2, BigDecimal.ROUND_HALF_UP)
						.doubleValue();
				orderByFenshu = avg3;
			}
			// 根据zlid查该项目
			if (null == this.review1DAO.findStateByZlid(zlid)) {
				ispass = "0";
			} else {
				ispass = this.review1DAO.findStateByZlid(zlid).toString();
			}
			// 放到model中
			basicModel.setSumFenshu(sumFenshu);
			basicModel.setZlid(info.getZlid());
			basicModel.setName(info.getName());
			basicModel.setZlqr(info.getZlqr());
			basicModel.setOrderByFenshu(orderByFenshu);
			basicModel.setIspass(ispass);
			basicModel.setCount(count);
			ermList10.add(basicModel);
		}
		return this.sortList(ermList10);
	}

	/**
	 * xyw 排序
	 * 
	 * @param oldList
	 * @return
	 */
	public List<BasicInfoModel> sortList(List<BasicInfoModel> oldList) {
		List<BasicInfoModel> tempList = oldList;
		BasicInfoModel tempBM;
		for (int i = 0; i < tempList.size(); i++) {
			Double maxFenshu = tempList.get(i).getOrderByFenshu();
			BasicInfoModel maxBI = tempList.get(i);
			int maxId = i;
			for (int j = i + 1; j < tempList.size(); j++) {
				if (oldList.get(j).getOrderByFenshu() > maxFenshu) {
					maxFenshu = tempList.get(j).getOrderByFenshu();
					maxBI = tempList.get(j);
					maxId = j;
				}
			}
			tempBM = tempList.get(i);
			tempList.remove(i);
			tempList.add(i, maxBI);
			tempList.remove(maxId);
			tempList.add(maxId, tempBM);
		}
		return tempList;
	}

	/**
	 * xyw排序分的方法
	 */
	public Map YY(String zlid, String hqlWhere) {
		Map map = new HashMap();
		// 根据专利id查出所有专家的ZJID的list集合也就是该专利在此学科的所有专家
		List<TblZhuanjiarukuxinxibiao> zhuanjiaList = this.getReview1DAO()
				.findZhuanjiaByZlid(zlid);
		List xmfs1List = null;
		double d1 = 0.0;
		// 对专家循环
		for (int x = 0; x < zhuanjiaList.size(); x++) {

			TblZhuanjiarukuxinxibiao zhuanjiarukuxinxibiao = (TblZhuanjiarukuxinxibiao) zhuanjiaList
					.get(x);
			int zjid = zhuanjiarukuxinxibiao.getFBh();// 专家zjid
			// 通过专家ID查询所有项目 根据权重分降序排序
			xmfs1List = this.getReview1DAO().findReviewXmListByZjId(zjid,
					"tx1." + hqlWhere);
			// int r = xmfs1List.size();
			int initFs = xmfs1List.size();
			// 对项目循环
			for (int y = 0; y < xmfs1List.size(); y++) {

				Object[] object = (Object[]) xmfs1List.get(y);
				Basicinfo basicinfo = (Basicinfo) object[0];
				// 权重分
				d1 = (Double) object[2];

				if (y > 0) {
					Object[] object2 = (Object[]) xmfs1List.get(y - 1);
					// 权重分
					double d2 = (Double) object2[2];

					if (d2 != d1) {
						initFs--;
					}
				}
				if (map.containsKey(basicinfo.getZlid())) {
					map.put(basicinfo.getZlid(), ((Integer) map.get(basicinfo
							.getZlid()))
							+ initFs);
				} else {
					map.put(basicinfo.getZlid(), initFs);
				}
			}
		}
		return map;
	}

	/**
	 * xyw 结果信息集
	 */
	public List findReviewResultsList(String hqlWhere) {
		List<TblXmjg1> tx1list1 = this.getReview1DAO().findReviewResultsList(
				hqlWhere);
		if (tx1list1.size() > 0) {
			List<XmReviewResults> ermList1 = new ArrayList<XmReviewResults>();
			for (TblXmjg1 xmjg1 : tx1list1) {

				if (null == xmjg1.getXmpjf() || null == xmjg1.getXmzf()
						|| null == xmjg1.getXmpjpxfx()) {
					ermList1.add(BeanAndModelConverter
							.getXmReviewResults1ByTblXmjg1(xmjg1));

				} else {
					double xmpjf = xmjg1.getXmpjf();
					double xmzf = xmjg1.getXmzf();
					double xmpjpxfx = xmjg1.getXmpjpxfx();

					// 求平均分留2位小数点
					BigDecimal bbb = new BigDecimal(xmpjf);
					BigDecimal ccc = new BigDecimal(xmzf);
					BigDecimal ddd = new BigDecimal(xmpjpxfx);
					double xmpjf1 = bbb.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
					double xmzf1 = ccc.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();

					double xmpjpxfx1 = ddd
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();

					xmjg1.setXmpjf(xmpjf1);
					xmjg1.setXmpjpxfx(xmpjpxfx1);
					xmjg1.setXmzf(xmzf1);
					ermList1.add(BeanAndModelConverter
							.getXmReviewResults1ByTblXmjg1(xmjg1));

				}
			}
			return ermList1;
		} else {
			return null;
		}
	}

	public boolean saveOrUpdateTblXmjg2(XmReviewResults2 xmReviewResults2,
			String jydjId) {
		return false;
	}

	public List<ExpertModel> listReviewExpertByBasicInfoId(String basicInfoId,
			String xkId, String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * xyw 根据zlid查出所有评审它的专家
	 * 
	 * @param hqlWhere
	 * @return
	 */
	public List findZhuanjiaByZlid(String zlid) {
		// TODO Auto-generated method stub
		List<TblZhuanjiarukuxinxibiao> zhuanjiaList = this.getReview1DAO()
				.findZhuanjiaByZlid(zlid);
		if (0 == zhuanjiaList.size()) {
			return null;
		} else {
			return zhuanjiaList;
		}
	}

	public XmReviewResults2 findXmReviewResults2ByXmId(String xmId) {
		// TODO Auto-generated method stub
		return null;
	}

	public PagerModel listReview2Result(String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	// 根据zlid查找项目信息
	public Object findXmByZlid(String zlid) {
		// TODO Auto-generated method stub
		this.review1DAO.findXmByZlid(zlid);
		return null;
	}

	/**
	 * xyw 根据专业id和 xkid查找专家个数
	 */

	public Integer findZjpsfpCountByXmpsmcId(String xmpsmcId, String xkId,
			String hqlWhere) {
		return this.review1DAO.findZjpsfpCountByXmpsmcId(xmpsmcId, xkId,
				hqlWhere);
	}

	/**
	 * xyw 根据xkid找基本信息表 项目的个数
	 */
	public Integer findXmCountByXkId(String xkId) {
		return this.review1DAO.findXmCountByXkId(xkId);
	}

	/**
	 * xyw 根据xkid找 分数表1 项目的个数
	 */
	public Integer findFsCountByXkId(String xkId) {
		return this.review1DAO.findFsCountByXkId(xkId);
	}

	public List<ExpertsReviewModel2> findReviewXmFsListByZlId(String zlid,
			String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	public void saveReviewsResults(Object o) {
		this.getReview1DAO().saveReviewsResults(o);

	}

	public void saveOrUpdateReviews1(Object o) {
		this.getReview1DAO().saveOrUpdateReviews1(o);
	}

	public List<Integer> listReviewFsExpertIdList(String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	public List listReviewXmIdList(int expertId, String hqlWhere) {
		// TODO Auto-generated method stub
		return null;
	}

	public File exportSecondReviewFs(String fileName,
			List<ExpertsReviewModel2> erm2List) {
		// TODO Auto-generated method stub
		return null;
	}
}